/**
 * @file   ReceivedTTF001File.java
 * @date   2012-11-8上午10:51:20
 * @author User
 */
package com.gzceb.zhgjj.mina.TTF001;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gzceb.zhgjj.mina.MinaConstants;
import com.gzceb.zhgjj.model.TTF001File;
import com.gzceb.zhgjj.util.PropertiesUtils;

/**
 * <p>TTF001文件接收类。</p>
 *
 * @date   2012-11-8上午10:51:20 
 * @author liqing
 * @description 收到LA/LB/LC/LD四种文件后，只负责保存文件
 */
public class ReceivedTTF001File extends IoHandlerAdapter {
	
	private  final Logger logger = LoggerFactory.getLogger(ReceivedTTF001File.class);
	private TTF001File tTF001File;
	
	/**
     * 新连接建立的时候触发
     */
    @Override
    public void sessionCreated(IoSession session) throws Exception {
       super.sessionCreated(session);
       tTF001File = new TTF001File(PropertiesUtils.getValue("destPath"));
    }

    /**
     * 新连接打开的时候触发，在sessionCreated之后被调用
     */
    @Override
    public void sessionOpened(IoSession session) throws Exception{
        super.sessionOpened(session);
    }
    /**
     * 连接被关闭的时候触发
     */
    @Override
    public void sessionClosed(IoSession session) throws Exception {
        
        super.sessionClosed(session);
    }
    /**
     * 有消息接收的时候触发,
     * 关键步骤。
     */
    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        //获取流数据
        //System.out.println("message :"+message);
        String rspStr = tTF001File.ProcRcvFile(message.toString());
        //如果接收的文件最后，中心已发来 f_over标示，银行不需再返回什么信号了
        if(! MinaConstants.F_OVER.getValue().equals(rspStr)){
        	session.write(rspStr);
        }
    }
   /**
    * 连接被闲置的时候触发
    */
    @Override
    public void sessionIdle(IoSession session, IdleStatus status)
            throws Exception {
    	logger.debug("关闭闲置session，ip：" + session.getRemoteAddress().toString());
    	session.close(false);
    }
    /**
     * 有异常抛出但是没有被catch的时候触发
     */
    @Override
    public void exceptionCaught(IoSession session,Throwable cause) throws Exception
    {
        //super.exceptionCaught(session, cause);
    	logger.debug("关闭异常 session，ip：" + session.getRemoteAddress().toString());
    	session.close(true);
    }
    /**
     * 消息发送的时候触发
     */
    @Override
    public void messageSent(IoSession session, Object message) throws Exception 
    {
        //System.out.println("messagesend!" + message);
        
    	super.messageSent(session, message);
        
    }
    
    public static void main(String arg[]){
    	//ApplicationContext ac = new ClassPathXmlApplicationContext("/spring/applicationContext-mina.xml");
    	//ReceivedTTF001File rf = (ReceivedTTF001File) ac.getBean("ReceivedTTF001File");
    	String fileCont="";
    	/*String tempstr = null;
    	try{
    		BufferedReader br = new BufferedReader(new FileReader("E:\\lq\\test.txt"));
    		while ((tempstr = br.readLine()) != null) {
    			fileCont += tempstr + System.getProperty("line.separator");
			}
    		
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	*/
    	fileCont = "";
    	if(fileCont.isEmpty())
    		System.out.println("yes");
    }
}
